﻿using Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Osg;
using OsgEarth;
using OsgEarth.Contrib;
using Tx.Addins;
using System.Windows.Interop;
using System.Drawing;
using Tx.FluentEx;
using Tx.osgEarth.UI.Commands.ViewCmds;
using Tx;
using Tx.osgEarth.UI.Commands.ImageLayerCmds;
using Tx.Sys;
using Tx.WPF.Utility;
using Tx.osgEarth;

namespace OsgEarthExample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : RibbonWindow, IMainWindow, IViewWindow
    {
        public static MainWindow Instance { get; private set; }

        public AppService App { get; private set; }

        public Ribbon Ribbon => ribbon;

        public MainWindow()
        {
            Instance = this;
            InitAppService();

            InitializeComponent();

            var osgEarthObj = this.osgEarthView.OsgEarthObj;

            var map = osgEarthObj.MapNode.Map;
            map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new TianDiTu(TianDiTu.Layer.img)), Name = nameof(TianDiTu.Layer.img) });
            map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new TianDiTu(TianDiTu.Layer.cia)), Name = nameof(TianDiTu.Layer.cia) });
            //map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new BingMap(BingMap.Layer.SatelliteMap)), Name = nameof(BingMap.Layer.SatelliteMap) });
            //map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new AMap(AMap.Layer.SatelliteMap)), Name = nameof(AMap.Layer.SatelliteMap) });
            //map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new AMap(AMap.Layer.MAP)), Name = nameof(AMap.Layer.MAP) });
            //map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(new AMap(AMap.Layer.Road)), Name = nameof(AMap.Layer.Road) });

            InitRibbon();

            ViewWindowActive?.Invoke(this);
        }

        private void InitRibbon()
        {
            var ribbonCreater = new RibbonCreater(ribbon, App);
            ribbonCreater.Tab("地球").Group("视图")
                .AddBotton<FlyToCmd>("飞往", "/Resources/paper-plane-icon.png");
            ribbonCreater.Tab("地球").Group("图层")
                .AddBotton<HideOrShowTiandituCiaCmd>("天地图矢量", "/Resources/maps.png");
        }

        private void InitAppService()
        {
            App = new AppService() { MainWindow = this };
            // 绑定预定义的消息
            App.Singleton<MessageProxy>().OnShow =
                delegate(string s, string s1, MsgBoxButtons arg3, MsgBoxIcon arg4)
                {
                    var result = TxDialogResult.Cancel;
                    this.SafeInvoke(() =>
                    {
                        result = (TxDialogResult)MessageBox.Show(this, s, s1, (MessageBoxButton)arg3,
                            (MessageBoxImage)arg4);
                    });
                    return result;
                };
            App.Singleton<MessageProxy>().OnShowAlert += delegate(string text, string caption)
            {
                this.SafeInvoke(() => { MessageBox.Show(this, text, caption); });
            };
            App.Singleton<ProgressProxy>().OnSetText += caption =>
            {
                this.SafeInvoke(() =>
                {
                    bool bRefresh = false;
                    if (caption != null && tbInfo.Text != caption)
                    {
                        tbInfo.Text = caption;
                        bRefresh = true;
                    }

                    //                     if (bRefresh)
                    //                     {
                    //                         Application.DoEvents();
                    //                     }
                });
            };
            App.Singleton<ProgressProxy>().OnSetValue += value =>
            {
                this.SafeInvoke(() =>
                {
                    //                     bool bRefresh = false;
                    //                     if (value > 0 && tsProgressBar.Value != value)
                    //                     {
                    TaskbarManager.Instance.SetProgressValue(value, 100, this.Handle);
                    // 
                    //                         tsProgressBar.Value = value;
                    //                         bRefresh = true;
                    //                     }
                    // 
                    //                     if (bRefresh)
                    //                     {
                    //                         statusStrip.Refresh();
                    //                         Application.DoEvents();
                    //                     }
                });
            };
            App.Singleton<ProgressProxy>().OnClear += () =>
            {
                this.SafeInvoke(() =>
                {
                    TaskbarManager.Instance.SetProgressValue(0, 100, this.Handle);

                    tbInfo.Text = "就绪";
                    //tsProgressBar.Value = 0;
                    //statusStrip.Refresh();
                    //Application.DoEvents();
                });
            };
        }

        #region IWindow

        /// <summary>Window句柄.</summary>
        /// <value>The handle.</value>
        public IntPtr Handle  => ((HwndSource)PresentationSource.FromDependencyObject(this)).Handle;

        /// <summary>图标.</summary>
        /// <value>The icon.</value>
        public object IconImage
        {
            get
            {
                return this.Icon;
            }
            set
            {
                this.Icon = value as ImageSource;
            }
        }

        #endregion

        #region IMainWindow


        /// <summary>Gets the active view window.</summary>
        /// <value>The active view window.</value>
        public object ActiveViewWindow => this;

        /// <summary>更新UI.</summary>
        public void RefreshUI()
        {
            this.InvalidateVisual();
        }

        /// <summary>Occurs when [view window active].</summary>
        public event Action<IViewWindow> ViewWindowActive;

        #endregion

        #region IViewWindow
        /// <summary>
        /// Gets the parent.
        /// </summary>
        /// <value>The parent.</value>
        public object ParentWindow => this.Owner;

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public object View => osgEarthView;

        #endregion
    }
}
